import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

# 数据：tx, ty → gx, gy

data = np.array([
    # #board
    # [193, 155, 233, 167],
    # [335, 94, 335, 124],
    # [293, 202, 306, 198],
    # [254, 303, 277, 271],
    # [397, 242, 379, 228],
    # #black
    # [432, 117, 475, 81],
    # [437, 238, 483, 266],
    # [436, 343, 476, 399],
    # #white
    # [183, 106, 132, 70],
    # [179, 221, 127, 236],
    # [179, 316, 129, 376]
    [312,231,271,108],
    [310,396,271,294],
    [295,584,246,509],
    [440,315,426,200],
    [438,497,417,412],
    [631,320,642,210],
    [641,509,661,427],
    [745,239,774,122],
    [744,403,774,308],
    [765,630,798,561]
    # [154, 46, 198, 89],
    # [138, 242, 190, 227],
    # [146, 403, 190, 352],
    # [498, 86, 459, 119],
    # [493, 251, 452, 231],
    # [491, 392, 449, 336],
    # [317, 103, 315	,62],
    # [310, 343, 306	,401]
    # [176, 113, 126, 81],
    # [171, 225, 122, 238],
    # [183.5, 330, 134.5, 379],
    # [449, 112, 492, 76],
    # [450, 218, 493, 232],
    # [444.5, 319, 487, 379],
    # [193, 89, 146, 46],
    # [311, 77, 306.5, 30],
    # [408, 82, 440.5, 34],
    # [195, 348, 150, 401],
    # [313, 343, 314, 349]
])
# 拆分
tx = data[:, 0].reshape(-1, 1)
ty = data[:, 1].reshape(-1, 1)
gx = data[:, 2]
gy = data[:, 3]

# 拟合
model_tx_to_gx = LinearRegression().fit(tx, gx)
model_ty_to_gy = LinearRegression().fit(ty, gy)

# 预测
gx_pred = model_tx_to_gx.predict(tx)
gy_pred = model_ty_to_gy.predict(ty)

# R² 计算
r2_tx_gx = model_tx_to_gx.score(tx, gx)
r2_ty_gy = model_ty_to_gy.score(ty, gy)

# 输出线性公式与R²
print(f"gx = {model_tx_to_gx.coef_[0]:.3f} * tx + {model_tx_to_gx.intercept_:.3f}   (R² = {r2_tx_gx:.4f})")
print(f"gy = {model_ty_to_gy.coef_[0]:.3f} * ty + {model_ty_to_gy.intercept_:.3f}   (R² = {r2_ty_gy:.4f})")

# 绘图
plt.figure(figsize=(12, 5))

plt.subplot(1, 2, 1)
plt.scatter(tx, gx, label='Actual gx')
plt.plot(tx, gx_pred, color='red', label='Fitted gx')
plt.xlabel('tx')
plt.ylabel('gx')
plt.title(f'tx → gx (R²={r2_tx_gx:.4f})')
plt.legend()

plt.subplot(1, 2, 2)
plt.scatter(ty, gy, label='Actual gy')
plt.plot(ty, gy_pred, color='green', label='Fitted gy')
plt.xlabel('ty')
plt.ylabel('gy')
plt.title(f'ty → gy (R²={r2_ty_gy:.4f})')
plt.legend()

plt.tight_layout()
plt.show()


# import numpy as np
# import matplotlib.pyplot as plt
# from sklearn.linear_model import LinearRegression

# # 数据 [tx, ty, gx, gy]
# data = np.array([
#     # [193, 155, 233, 167],
#     # [335, 94, 335, 124],
#     # [293, 202, 306, 198],
#     # [254, 303, 277, 271],
#     # [397, 242, 379, 228],
#     # [432, 117, 475, 81],
#     # [437, 238, 483, 266],
#     # [436, 343, 476, 399],
#     # [183, 106, 132, 70],
#     # [179, 221, 127, 236],
#     # [179, 316, 129, 376],
#     [312,231,271,108],
#     [310,396,271,294],
#     [295,584,246,509],
#     [440,315,426,200],
#     [438,497,417,412],
#     [631,320,642,210],
#     [641,509,661,427],
#     [745,239,774,122],
#     [744,403,774,308],
#     [765,630,798,561]
# ])

# tx = data[:, 0]
# ty = data[:, 1]
# gx = data[:, 2]
# gy = data[:, 3]

# # 构建仿射变换输入 [tx, ty, 1]
# X = np.vstack([tx, ty, np.ones_like(tx)]).T

# # 拟合仿射模型：gx 和 gy 分别建模
# model_gx = LinearRegression(fit_intercept=False).fit(X, gx)
# model_gy = LinearRegression(fit_intercept=False).fit(X, gy)

# # 预测
# gx_pred = model_gx.predict(X)
# gy_pred = model_gy.predict(X)

# # R²
# r2_gx = model_gx.score(X, gx)
# r2_gy = model_gy.score(X, gy)

# # 打印仿射系数
# print("仿射变换公式：")
# print(f"gx = {model_gx.coef_[0]:.4f} * tx + {model_gx.coef_[1]:.4f} * ty + {model_gx.coef_[2]:.4f}")
# print(f"gy = {model_gy.coef_[0]:.4f} * tx + {model_gy.coef_[1]:.4f} * ty + {model_gy.coef_[2]:.4f}")
# print(f"R² gx: {r2_gx:.4f}, R² gy: {r2_gy:.4f}")

# # 按 gx 大小排序后画图
# sorted_gx_idx = np.argsort(gx)
# sorted_gy_idx = np.argsort(gy)

# plt.figure(figsize=(12, 5))

# # gx vs 预测
# plt.subplot(1, 2, 1)
# plt.plot(gx[sorted_gx_idx], label='gx actual', marker='o')
# plt.plot(gx_pred[sorted_gx_idx], label='gx predicted', linestyle='--')
# plt.title(f'gx Actual vs Predicted (R²={r2_gx:.4f})')
# plt.xlabel('Sorted index by gx')
# plt.ylabel('gx value')
# plt.legend()

# # gy vs 预测
# plt.subplot(1, 2, 2)
# plt.plot(gy[sorted_gy_idx], label='gy actual', marker='o')
# plt.plot(gy_pred[sorted_gy_idx], label='gy predicted', linestyle='--')
# plt.title(f'gy Actual vs Predicted (R²={r2_gy:.4f})')
# plt.xlabel('Sorted index by gy')
# plt.ylabel('gy value')
# plt.legend()

# plt.tight_layout()
# plt.show()
